home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xless / xless141.z / xless141 / xless-1.4.1 / callbacks.c < prev    next >
C/C++ Source or Header  |  1993-05-05  |  8KB  |  319 lines

  1. /*
  2.  * Copyright 1989 Massachusetts Institute of Technology
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of M.I.T. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  M.I.T. makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  15.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  16.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  18.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  19.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20.  *
  21.  * Author: Carlo Lisa
  22.  *       MIT Project Athena
  23.  *       carlo@athena.mit.edu
  24.  *
  25.  * $Header: /usr/sww/share/src/X11R5/local/clients/xless-1.4/RCS/callbacks.c,v 1.22 1993/03/02 01:57:10 dglo Exp dglo $
  26.  */
  27.  
  28. #include "xless.h"
  29.  
  30. /* buffers for all dialog boxes */
  31. static char search_buffer[MAX_INPUT];
  32. static char newwin_buffer[MAX_INPUT];
  33. static char chgfile_buffer[MAX_INPUT];
  34.  
  35. /*
  36.  * Button callback functions.
  37.  */
  38.  
  39. void
  40. Quit(widget, closure, callData)
  41. Widget widget;
  42. XtPointer closure;
  43. XtPointer callData;
  44. {
  45.   QuitFunction();
  46. }
  47.  
  48. void
  49. Cancel(widget, closure, callData)
  50. Widget widget;
  51. XtPointer closure;
  52. XtPointer callData;
  53. {
  54.   XtPopdown((Widget)closure);
  55. }
  56.  
  57. void
  58. CallEditor(widget, closure, callData)
  59. Widget widget;
  60. XtPointer closure;
  61. XtPointer callData;
  62. {
  63.   WindowInfo *wi = (WindowInfo *)closure;
  64.   const char *editor;
  65.   int cmdlen = 0;
  66.   char *cmd;
  67.  
  68.   /* figure out which editor they want */
  69.   if ((editor = getenv(EDITORVAR)) == NULL)
  70.     editor = DEFEDITOR;
  71.  
  72.   /* get a buffer long enough for the entire command */
  73.   if (!resources.editorDoesWindows)
  74.     cmdlen = 9;            /* "xterm -e " */
  75.   cmdlen += strlen(editor) + 1 + strlen(wi->file) + 2;
  76.   if (cmd = (char *)malloc(cmdlen)) {
  77.  
  78.     /* don't start from an xterm if the editor has its own window */
  79.     if (resources.editorDoesWindows)
  80.       strcpy(cmd, editor);
  81.     else {
  82.       strcpy(cmd, "xterm -e ");
  83.       strcat(cmd, editor);
  84.     }
  85.     strcat(cmd, " ");
  86.     strcat(cmd, wi->file);
  87.     strcat(cmd, "&");
  88.     system(cmd);
  89.     free(cmd);
  90.   }
  91. }
  92.  
  93. void
  94. Reload(widget, closure, callData)
  95. Widget widget;
  96. XtPointer closure;
  97. XtPointer callData;
  98. {
  99.   WindowInfo *wi = (WindowInfo *)closure;
  100.   FILE *file;
  101.   char *new_data;
  102.  
  103.   if ((file = fopen(wi->file, "r")) == NULL)
  104.     CouldntOpen(wi->base, wi->file);
  105.   else {
  106.     new_data = InitData(file);
  107.     free(wi->memory);
  108.     SetReadText(wi->text, new_data);
  109.     wi->memory = new_data;
  110.   }
  111. }
  112.  
  113. void
  114. SearchNext(widget, closure, callData)
  115. Widget widget;
  116. XtPointer closure;
  117. XtPointer callData;
  118. {
  119.   WindowInfo *wi = (WindowInfo *)closure;
  120.  
  121.   DoSearch(wi->text, wi->memory, search_buffer, wi->base);
  122. }
  123.  
  124. static void
  125. popdownAndSearch(widget, closure, callData)
  126. Widget widget;
  127. XtPointer closure;
  128. XtPointer callData;
  129. {
  130.   WindowInfo *wi = (WindowInfo *)closure;
  131.  
  132.   XtPopdown((Widget)wi->search_popup);
  133.   DoSearch(wi->text, wi->memory, search_buffer, wi->base);
  134. }
  135.  
  136. void
  137. Search(widget, closure, callData)
  138. Widget widget;
  139. XtPointer closure;
  140. XtPointer callData;
  141. {
  142.   WindowInfo *wi = (WindowInfo *)closure;
  143.  
  144.   /* build dialog box if it doesn't exist */
  145.   if (!wi->search_popup)
  146.     wi->search_popup = DialogBox(wi->base, popdownAndSearch, (XtPointer )wi,
  147.                  "Search for:", "Search", search_buffer);
  148.  
  149.   /* popup search dialog box */
  150.   SetPopup(wi->base, wi->search_popup);
  151. }
  152.  
  153. static void
  154. popdownAndChange(widget, closure, callData)
  155. Widget widget;
  156. XtPointer closure;
  157. XtPointer callData;
  158. {
  159.   WindowInfo *wi = (WindowInfo *)closure;
  160.   FILE *file;
  161.   char *new_data;
  162.   const char *filename;
  163.   XLessFlag flag;
  164.   Arg args[1];
  165.  
  166.   XtPopdown((Widget)wi->chgfile_popup);
  167.  
  168.   /* make sure there's a filename there */
  169.   filename = chgfile_buffer;
  170.   if (*filename == 0)
  171.     return;
  172.  
  173. #ifdef TILDE_EXPANSION
  174.     /* see if we need to do tilde expansion */
  175.     if (filename && *filename == '~') {
  176.       filename = TildeExpand(filename);
  177.       if (*filename != '~')
  178.     flag |= XLessFreeFilename;
  179.     }
  180. #endif /* TILDE_EXPANSION */
  181.  
  182.   if ((file = fopen(filename, "r")) == NULL)
  183.     CouldntOpen(wi->base, filename);
  184.   else {
  185.  
  186.     /* read in new file */
  187.     new_data = InitData(file);
  188.     free(wi->memory);
  189.     if (wi->flag & XLessFreeFilename) {
  190.       free(wi->file);
  191.       wi->flag &= ~XLessFreeFilename;
  192.     }
  193.     SetReadText(wi->text, new_data);
  194.     wi->memory = new_data;
  195.     wi->file = filename;
  196.     wi->flag |= flag;
  197.  
  198.     /* sensitize buttons if previous file was STDIN */
  199.     XtSetArg(args[0], XtNsensitive, True);
  200.     if (wi->editor_button) {
  201.       XtSetValues(wi->editor_button, args, 1);
  202.       wi->editor_button = 0;
  203.     }
  204.     if (wi->reload_button) {
  205.       XtSetValues(wi->reload_button, args, 1);
  206.       wi->editor_button = 0;
  207.     }
  208.  
  209.     /* set title & icon name */
  210.     SetXNames(wi->base, filename);
  211.   }
  212. }
  213.  
  214. void
  215. ChangeFile(widget, closure, callData)
  216. Widget widget;
  217. XtPointer closure;
  218. XtPointer callData;
  219. {
  220.   WindowInfo *wi = (WindowInfo *)closure;
  221.  
  222.   if (!wi->chgfile_popup)
  223.     wi->chgfile_popup = DialogBox(wi->base, popdownAndChange, (XtPointer)wi,
  224.                   "Enter filename:", "Ok", chgfile_buffer);
  225.  
  226.   SetPopup(wi->base, wi->chgfile_popup);    /* Change file dialog box */
  227. }
  228.  
  229. static void
  230. popdownAndCreate(widget, closure, callData)
  231. Widget widget;
  232. XtPointer closure;
  233. XtPointer callData;
  234. {
  235.   WindowInfo *wi = (WindowInfo *)closure;
  236.  
  237.   XtPopdown((Widget)wi->newwin_popup);
  238.   CreateWindow(wi->base, newwin_buffer);
  239. }
  240.  
  241. void
  242. NewWindow(widget, closure, callData)
  243. Widget widget;
  244. XtPointer closure;
  245. XtPointer callData;
  246. {
  247.   WindowInfo *wi = (WindowInfo *)closure;
  248.  
  249.   if (!wi->newwin_popup)
  250.     wi->newwin_popup = DialogBox(wi->base, popdownAndCreate, (XtPointer)wi,
  251.             "Enter filename:", "Ok", newwin_buffer);
  252.   SetPopup(wi->base, wi->newwin_popup);    /* New window dialog box */
  253. }
  254.  
  255. void
  256. CloseWindow(widget, closure, callData)
  257. Widget widget;
  258. XtPointer closure;
  259. XtPointer callData;
  260. {
  261.   WindowInfo *sd = (WindowInfo *)closure;
  262.  
  263.   DestroyWindowInfo(sd);
  264. }
  265.  
  266. void
  267. Print(widget, closure, callData)
  268. Widget widget;
  269. XtPointer closure;
  270. XtPointer callData;
  271. {
  272.   WindowInfo *wi = (WindowInfo *)closure;
  273.   FILE *file;
  274.   const char *printfile;
  275.   char *cmd;
  276. #ifdef WIMPY_STDIO
  277.   char *line, *l;
  278. #endif
  279.  
  280.   if (wi->file == NULL) {
  281.  
  282.     /* create a temporary file */
  283.     printfile = tmpnam(0);
  284.     file = fopen(printfile, "w");
  285.  
  286. #ifdef WIMPY_STDIO
  287.     /* write stdin (line by line) to temporary file */
  288.     line = l = wi->memory;
  289.     while (line && *line) {
  290.       if (l = strchr(line, '\n'))
  291.     *l = 0;
  292.       fprintf(file, "%s\n", line);
  293.       if (l)
  294.     *l = '\n';
  295.       line = l+1;
  296.     }
  297. #else /* !WIMPY_STDIO */
  298.     /* write stdin (in one manly hunk) to temp file */
  299.     fputs(wi->memory, file);
  300. #endif /* WIMPY_STDIO */
  301.     fclose(file);
  302.   } else
  303.     printfile = wi->file;
  304.  
  305.   /* create the print command string */
  306.   if (cmd = (char *)malloc(strlen(resources.printCmd) + 1 +
  307.                strlen(printfile) + 1)) {
  308.     strcpy(cmd, resources.printCmd);
  309.     strcat(cmd, " ");
  310.     strcat(cmd, printfile);
  311.     system(cmd);
  312.     free(cmd);
  313.   }
  314.  
  315.   /* unlink the file if we created it */
  316.   if (wi->file == NULL)
  317.     unlink(printfile);
  318. }
  319.